
<!DOCTYPE HTML>
<html lang="cn" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>函数 · 前端学习笔记</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="chengdaohua">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-anchors/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-toggle-chapters/toggle.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-advanced-emoji/emoji-website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="string.html" />
    
    
    <link rel="prev" href="object.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="https://chengdaohua.gitee.io/remarkbook/" target="_blank" class="custom-link">Home</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../vue/">
            
                <a href="../vue/">
            
                    
                    vue 学习
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../vue/start.html">
            
                <a href="../vue/start.html">
            
                    
                    原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../vue/mokuaihua.html">
            
                <a href="../vue/mokuaihua.html">
            
                    
                    模块化和组件化开发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../vue/huakucha.html">
            
                <a href="../vue/huakucha.html">
            
                    
                    花裤衩项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../vue/chushihua.html">
            
                <a href="../vue/chushihua.html">
            
                    
                    项目初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../vue/guifan.html">
            
                <a href="../vue/guifan.html">
            
                    
                    代码规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="jsReadme.html">
            
                <a href="jsReadme.html">
            
                    
                    javaScript
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="array.html">
            
                <a href="array.html">
            
                    
                    数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="object.html">
            
                <a href="object.html">
            
                    
                    对象
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.3.3" data-path="function.html">
            
                <a href="function.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="string.html">
            
                <a href="string.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../review/project.html">
            
                <a href="../review/project.html">
            
                    
                    review
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../review/CodeReview.html">
            
                <a href="../review/CodeReview.html">
            
                    
                    CoderReview
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../node/简介.html">
            
                <a href="../node/简介.html">
            
                    
                    node
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../node/简介.html">
            
                <a href="../node/简介.html">
            
                    
                    简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../node/express.html">
            
                <a href="../node/express.html">
            
                    
                    express
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../ts/ts.html">
            
                <a href="../ts/ts.html">
            
                    
                    ts
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../websocket/websocket.html">
            
                <a href="../websocket/websocket.html">
            
                    
                    websocket
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../webpack/webpack.html">
            
                <a href="../webpack/webpack.html">
            
                    
                    webpack
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >函数</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="js-&#x51FD;&#x6570;"><a name="js-&#x51FD;&#x6570;" class="plugin-anchor" href="#js-&#x51FD;&#x6570;"><i class="fa fa-link" aria-hidden="true"></i></a>js &#x51FD;&#x6570;</h1>
<p><strong>JavaScript &#x51FD;&#x6570;&#x662F;&#x88AB;&#x8BBE;&#x8BA1;&#x4E3A;&#x6267;&#x884C;&#x7279;&#x5B9A;&#x4EFB;&#x52A1;&#x7684;&#x4EE3;&#x7801;&#x5757;, JavaScript &#x51FD;&#x6570;&#x4F1A;&#x5728;&#x67D0;&#x4EE3;&#x7801;&#x8C03;&#x7528;&#x5B83;&#x65F6;&#x88AB;&#x6267;&#x884C;</strong></p>
<h3 id="&#x51FD;&#x6570;&#x4E2D;&#x5B9A;&#x4E49;&#x65B9;&#x5F0F;--&#x8C03;&#x7528;&#x65B9;&#x5F0F;"><a name="&#x51FD;&#x6570;&#x4E2D;&#x5B9A;&#x4E49;&#x65B9;&#x5F0F;--&#x8C03;&#x7528;&#x65B9;&#x5F0F;" class="plugin-anchor" href="#&#x51FD;&#x6570;&#x4E2D;&#x5B9A;&#x4E49;&#x65B9;&#x5F0F;--&#x8C03;&#x7528;&#x65B9;&#x5F0F;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x51FD;&#x6570;&#x4E2D;&#x5B9A;&#x4E49;&#x65B9;&#x5F0F; &amp; &#x8C03;&#x7528;&#x65B9;&#x5F0F;</h3>
<h4 id="&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x65B9;&#x5F0F;"><a name="&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x65B9;&#x5F0F;" class="plugin-anchor" href="#&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x65B9;&#x5F0F;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x65B9;&#x5F0F;</h4>
<p>&#x51FD;&#x6570;&#x5173;&#x952E;&#x5B57;(function)&#x8BED;&#x53E5;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">//ES5</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>)</span>{}<span class="hljs-comment">//&#x533F;&#x540D;&#x51FD;&#x6570;</span>
<span class="hljs-comment">//ES6</span>
()=&gt;{}<span class="hljs-comment">//&#x5982;&#x679C;{}&#x5185;&#x5BB9;&#x53EA;&#x6709;&#x4E00;&#x884C;{}&#x548C;return&#x5173;&#x952E;&#x5B57;&#x53EF;&#x7701;,</span>
</code></pre>
<p>&#x51FD;&#x6570;&#x5B57;&#x9762;&#x91CF;/&#x51FD;&#x6570;&#x8868;&#x8FBE;&#x5F0F;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">//ES5</span>
<span class="hljs-keyword">var</span> sum = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{};
<span class="hljs-comment">//ES6</span>
<span class="hljs-keyword">let</span> sum = () =&gt; {}; <span class="hljs-comment">//&#x5982;&#x679C;{}&#x5185;&#x5BB9;&#x53EA;&#x6709;&#x4E00;&#x884C;{}&#x548C;return&#x5173;&#x952E;&#x5B57;&#x53EF;&#x7701;,</span>
</code></pre>
<p>Function()&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-javascript">var fnMethodName = new Function(&#x2018;x&apos;,&apos;alert(x);&apos;) // &#x7531;Function&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#x4E2A;&#x6570;&#x53EF;&#x53D8;&#x3002;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x5199;&#x51FD;&#x6570;&#x4F53;&#xFF0C;&#x524D;&#x9762;&#x7684;&#x53C2;&#x6570;&#x5199;&#x5165;&#x53C2;&#x3002;
</code></pre>
<p>1.&#x51FD;&#x6570;&#x58F0;&#x660E;&#x6709;&#x9884;&#x89E3;&#x6790;,&#x800C;&#x4E14;&#x51FD;&#x6570;&#x58F0;&#x660E;&#x7684;&#x4F18;&#x5148;&#x7EA7;&#x9AD8;&#x4E8E;&#x53D8;&#x91CF;; 2.&#x4F7F;&#x7528; Function &#x6784;&#x9020;&#x51FD;&#x6570;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x7684;&#x65B9;&#x5F0F;&#x662F;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x8868;&#x8FBE;&#x5F0F;,&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x4F1A;&#x5BFC;&#x81F4;&#x89E3;&#x6790;&#x4E24;&#x6B21;&#x4EE3;&#x7801;&#xFF0C;&#x5F71;&#x54CD;&#x6027;&#x80FD;&#x3002;&#x7B2C;&#x4E00;&#x6B21;&#x89E3;&#x6790;&#x5E38;&#x89C4;&#x7684; js &#x7801;&#xFF0C;&#x7B2C;&#x4E8C;&#x6B21;&#x89E3;&#x6790;&#x4F20;&#x5165;&#x6784;&#x9020;&#x51FD;&#x6570;&#x7684;&#x5B57;&#x7B26;&#x4E32;</p>
<h3 id="&#x51FD;&#x6570;&#x4F5C;&#x7528;&#x57DF;-&#x5168;&#x5C40;&#x4F5C;&#x7528;&#x57DF;&#x548C;&#x51FD;&#x6570;&#x4F5C;&#x7528;&#x57DF;&#xFF0C;&#x6CA1;&#x6709;&#x5757;&#x7EA7;&#x4F5C;&#x7528;&#x57DF;"><a name="&#x51FD;&#x6570;&#x4F5C;&#x7528;&#x57DF;-&#x5168;&#x5C40;&#x4F5C;&#x7528;&#x57DF;&#x548C;&#x51FD;&#x6570;&#x4F5C;&#x7528;&#x57DF;&#xFF0C;&#x6CA1;&#x6709;&#x5757;&#x7EA7;&#x4F5C;&#x7528;&#x57DF;" class="plugin-anchor" href="#&#x51FD;&#x6570;&#x4F5C;&#x7528;&#x57DF;-&#x5168;&#x5C40;&#x4F5C;&#x7528;&#x57DF;&#x548C;&#x51FD;&#x6570;&#x4F5C;&#x7528;&#x57DF;&#xFF0C;&#x6CA1;&#x6709;&#x5757;&#x7EA7;&#x4F5C;&#x7528;&#x57DF;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x51FD;&#x6570;&#x4F5C;&#x7528;&#x57DF; &#x5168;&#x5C40;&#x4F5C;&#x7528;&#x57DF;&#x548C;&#x51FD;&#x6570;&#x4F5C;&#x7528;&#x57DF;&#xFF0C;&#x6CA1;&#x6709;&#x5757;&#x7EA7;&#x4F5C;&#x7528;&#x57DF;</h3>
<p>&#x5728; JavaScript &#x4E2D;&#x4F7F;&#x7528;&#x7684;&#x662F;&#x51FD;&#x6570;&#x4F5C;&#x7528;&#x57DF;&#xFF0C;&#x53D8;&#x91CF;&#x5728;&#x58F0;&#x660E;&#x5B83;&#x4EEC;&#x7684;&#x51FD;&#x6570;&#x4F53;&#x4EE5;&#x53CA;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x4F53;&#x5D4C;&#x5957;&#x7684;&#x4EFB;&#x610F;&#x51FD;&#x6570;&#x4F53;&#x90FD;&#x662F;&#x6709;&#x5B9A;&#x4E49;&#x7684;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> i = <span class="hljs-number">5</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">func</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(i);
}

func(); <span class="hljs-comment">// 5</span>

&#x53D8;&#x91CF;&#x63D0;&#x5347;&#x5BFC;&#x81F4;&#x5185;&#x5C42;&#x53D8;&#x91CF;&#x53EF;&#x80FD;&#x4F1A;&#x8986;&#x76D6;&#x5916;&#x5C42;&#x53D8;&#x91CF;;

<span class="hljs-keyword">var</span> i = <span class="hljs-number">5</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">func</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(i);

  <span class="hljs-keyword">if</span> (<span class="hljs-literal">true</span>) {
    <span class="hljs-keyword">var</span> i = <span class="hljs-number">6</span>;
  }
}

func(); <span class="hljs-comment">// undefined</span>
</code></pre>
<h3 id="&#x51FD;&#x6570;&#x4F5C;&#x7528;&#x57DF;&#x94FE;"><a name="&#x51FD;&#x6570;&#x4F5C;&#x7528;&#x57DF;&#x94FE;" class="plugin-anchor" href="#&#x51FD;&#x6570;&#x4F5C;&#x7528;&#x57DF;&#x94FE;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x51FD;&#x6570;&#x4F5C;&#x7528;&#x57DF;&#x94FE;</h3>
<p>js &#x7CFB;&#x5217;&#x535A;&#x5BA2; <a href="https://www.cnblogs.com/wangfupeng1988/p/3977924.html" target="_blank">https://www.cnblogs.com/wangfupeng1988/p/3977924.html</a></p>
<h3 id="&#x9884;&#x89E3;&#x6790;----&#x53D8;&#x91CF;&#x63D0;&#x5347;---&#x9884;&#x89E3;&#x6790;&#x8FC7;&#x7A0B;&#xFF1A;"><a name="&#x9884;&#x89E3;&#x6790;----&#x53D8;&#x91CF;&#x63D0;&#x5347;---&#x9884;&#x89E3;&#x6790;&#x8FC7;&#x7A0B;&#xFF1A;" class="plugin-anchor" href="#&#x9884;&#x89E3;&#x6790;----&#x53D8;&#x91CF;&#x63D0;&#x5347;---&#x9884;&#x89E3;&#x6790;&#x8FC7;&#x7A0B;&#xFF1A;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x9884;&#x89E3;&#x6790; -- &#x53D8;&#x91CF;&#x63D0;&#x5347; --&#x9884;&#x89E3;&#x6790;&#x8FC7;&#x7A0B;&#xFF1A;</h3>
<p>&#x628A;&#x53D8;&#x91CF;&#x7684;&#x58F0;&#x660E;&#x63D0;&#x5347;&#x5230;&#x5F53;&#x524D;&#x4F5C;&#x7528;&#x57DF;&#x7684;&#x6700;&#x524D;&#x9762;&#xFF0C;&#x53EA;&#x4F1A;&#x63D0;&#x5347;&#x58F0;&#x660E;&#xFF0C;&#x4E0D;&#x4F1A;&#x63D0;&#x5347;&#x8D4B;&#x503C;&#x3002;
&#x628A;&#x51FD;&#x6570;&#x7684;&#x58F0;&#x660E;&#x63D0;&#x5347;&#x5230;&#x5F53;&#x524D;&#x4F5C;&#x7528;&#x57DF;&#x7684;&#x6700;&#x524D;&#x9762;&#xFF0C;&#x53EA;&#x4F1A;&#x63D0;&#x5347;&#x58F0;&#x660E;&#xFF0C;&#x4E0D;&#x4F1A;&#x63D0;&#x5347;&#x8C03;&#x7528;&#x3002;
&#x5148;&#x63D0;&#x5347; var&#xFF0C;&#x518D;&#x63D0;&#x5347; function&#x3002;
&#x63D0;&#x5347;&#x5B8C;&#x540E;&#x5176;&#x4ED6;&#x4EE3;&#x7801;&#x4F4D;&#x7F6E;&#x4E0D;&#x53D8;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> a = <span class="hljs-number">25</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">abc</span>(<span class="hljs-params"></span>) </span>{
  alert(a);
  <span class="hljs-keyword">var</span> a = <span class="hljs-number">10</span>;
}
abc();

&#x9884;&#x89E3;&#x6790;&#x540E;&#x5982;&#x4E0B;&#xFF1A;
<span class="hljs-keyword">var</span> a;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">abc</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span> a;
    alert(a);
    a = <span class="hljs-number">10</span>;
}
a = <span class="hljs-number">25</span>;
abc();  <span class="hljs-comment">//undefined;</span>
</code></pre>
<h4 id="&#x8C03;&#x7528;&#x65B9;&#x5F0F;"><a name="&#x8C03;&#x7528;&#x65B9;&#x5F0F;" class="plugin-anchor" href="#&#x8C03;&#x7528;&#x65B9;&#x5F0F;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x8C03;&#x7528;&#x65B9;&#x5F0F;</h4>
<p>&#x51FD;&#x6570;&#x8C03;&#x7528;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fn</span>(<span class="hljs-params">x, y</span>) </span>{
  <span class="hljs-comment">//&#x5B9A;&#x4E49;&#x51FD;&#x6570;</span>
  <span class="hljs-built_in">console</span>.log(x * y); <span class="hljs-comment">//&#x8FD4;&#x56DE;&#x503C;</span>
}
<span class="hljs-built_in">console</span>.log(fn(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>));
</code></pre>
<p>&#x65B9;&#x6CD5;&#x8C03;&#x7528;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> obj = {
  value: <span class="hljs-number">0</span>,
  increment: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">num</span>) </span>{
    <span class="hljs-keyword">this</span>.value += <span class="hljs-keyword">typeof</span> num === <span class="hljs-string">&apos;number&apos;</span> ? num : <span class="hljs-number">1</span>;
  }
};
obj.increment();
<span class="hljs-built_in">console</span>.log(obj.value); <span class="hljs-comment">//1</span>
obj.increment(<span class="hljs-number">2</span>);
<span class="hljs-built_in">console</span>.log(obj.value); <span class="hljs-comment">//2</span>
</code></pre>
<p>&#x6784;&#x9020;&#x51FD;&#x6570;&#x7ED3;(new &#x547D;&#x4EE4;&#x95F4;&#x63A5;&#x8C03;&#x7528;)</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fn</span>(<span class="hljs-params">x, y</span>) </span>{
  <span class="hljs-comment">//&#x5B9A;&#x4E49;&#x51FD;&#x6570;</span>
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;x =&apos;</span> + x + <span class="hljs-string">&apos;, y =&apos;</span> + y);
}
<span class="hljs-keyword">var</span> f1 = <span class="hljs-keyword">new</span> fn(<span class="hljs-number">3</span>, <span class="hljs-number">4</span>);

<span class="hljs-comment">// &#x4F7F;&#x7528; new &#x547D;&#x4EE4;&#x8C03;&#x7528;&#x51FD;&#x6570;&#x65F6;&#xFF0C;&#x8FD4;&#x56DE;&#x7684;&#x662F;&#x5BF9;&#x8C61;&#xFF0C;&#x800C;&#x4E0D;&#x662F; return &#x7684;&#x8FD4;&#x56DE;&#x503C;&#x3002;&#x5982;&#x679C;&#x4E0D;&#x9700;&#x8981;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;&#x6216;&#x8005; return &#x7684;&#x8FD4;&#x56DE;&#x503C;&#x662F;&#x5BF9;&#x8C61;&#xFF0C;&#x5219;&#x53EF;&#x4EE5;&#x9009;&#x7528; new &#x95F4;&#x63A5;&#x8C03;&#x7528;&#x51FD;&#x6570;&#x3002;</span>
</code></pre>
<p>&#x4E0A;&#x4E0B;&#x6587;&#x8C03;&#x7528;&#x6A21;&#x5F0F; call/apply</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fn</span>(<span class="hljs-params">x, y</span>) </span>{
  <span class="hljs-comment">//&#x5B9A;&#x4E49;&#x6C42;&#x548C;&#x51FD;&#x6570;</span>
  <span class="hljs-keyword">return</span> x + y;
}
<span class="hljs-built_in">console</span>.log(fn.call(<span class="hljs-literal">null</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>));
</code></pre>
<p>&#x533F;&#x540D;&#x81EA;&#x6267;&#x884C;</p>
<pre><code class="lang-javascript">(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-number">1</span>);
})()(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-number">1</span>);
})();
</code></pre>
<h3 id="&#x51FD;&#x6570;&#x7684;&#x5E38;&#x7528;&#x5C5E;&#x6027;"><a name="&#x51FD;&#x6570;&#x7684;&#x5E38;&#x7528;&#x5C5E;&#x6027;" class="plugin-anchor" href="#&#x51FD;&#x6570;&#x7684;&#x5E38;&#x7528;&#x5C5E;&#x6027;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x51FD;&#x6570;&#x7684;&#x5E38;&#x7528;&#x5C5E;&#x6027;</h3>
<p>1.arguments</p>
<pre><code class="lang-javascript">ES5;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">arg</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">arguments</span>.length, <span class="hljs-built_in">arguments</span>[<span class="hljs-number">0</span>], <span class="hljs-built_in">arguments</span>[<span class="hljs-number">1</span>]);
} <span class="hljs-comment">// 2 1 2</span>

ES6&#x5269;&#x4F59;&#x53C2;&#x6570;;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">func</span>(<span class="hljs-params">a, ...rest</span>) </span>{
  <span class="hljs-built_in">console</span>.log(a);
  <span class="hljs-built_in">console</span>.log(rest);
}
func(<span class="hljs-number">1</span>);
func(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>);
</code></pre>
<p>2.prototype:&#x521B;&#x5EFA;&#x81EA;&#x5B9A;&#x4E49;&#x5F15;&#x7528;&#x7C7B;&#x578B;&#x4EE5;&#x53CA;&#x5B9E;&#x73B0;&#x7EE7;&#x627F;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Fn</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">this</span>.name = <span class="hljs-string">&apos;&#x4F60;&#x597D;&apos;</span>;
}
Fn.prototype.name = <span class="hljs-string">&apos;&#x4F60;&#x597D;1&apos;</span>;
Fn.prototype.getName = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.name;
};
<span class="hljs-keyword">var</span> f = <span class="hljs-keyword">new</span> Fn();
f.name; <span class="hljs-comment">//&#x8F93;&#x51FA; &apos;&#x4F60;&#x597D;&apos;</span>
f.getName(); <span class="hljs-comment">//&#x8F93;&#x51FA; &apos;&#x4F60;&#x597D;&apos;</span>
Fn.prototype.getName(); <span class="hljs-comment">//&#x8F93;&#x51FA; &apos;&#x4F60;&#x597D;1&apos;</span>
</code></pre>
<p>2.&#x7C7B;&#x6570;&#x7EC4;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fn</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">arguments</span>);
}
fn(); <span class="hljs-comment">// Arguments [callee: &#x192;, Symbol(Symbol.iterator): &#x192;]</span>
</code></pre>
<p>&#x51FD;&#x6570;&#x5E38;&#x7528;&#x65B9;&#x6CD5;
1.apply()&#x548C; call() &#x5728;&#x7279;&#x5B9A;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#x4E2D;&#x8C03;&#x7528;&#x51FD;&#x6570;,&#x7B49;&#x4E8E;&#x8BBE;&#x7F6E;&#x51FD;&#x6570;&#x4F53;&#x5185; this &#x5BF9;&#x8C61;&#x7684;&#x503C;
2.bind() &#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x7684;&#x5B9E;&#x4F8B;,&#x5176; this &#x7684;&#x503C;&#x4F1A;&#x88AB;&#x7ED1;&#x5B9A;&#x5230;&#x4F20;&#x7ED9; bind()&#x51FD;&#x6570;&#x7684;&#x503C;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">add</span>(<span class="hljs-params">a, b</span>) </span>{
  <span class="hljs-keyword">return</span> a + b;
}
<span class="hljs-keyword">var</span> newFoo = add.bind(<span class="hljs-keyword">this</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>);
newFoo();
</code></pre>
<h3 id="&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;"><a name="&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;" class="plugin-anchor" href="#&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;</h3>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fn</span>(<span class="hljs-params">x, y, callback</span>) </span>{
  callback(x, y);
}

fn(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">x, y</span>) </span>{
  <span class="hljs-built_in">console</span>.log(x + y);
});


&#x51FD;&#x6570;&#x662F;&#x4E00;&#x79CD;&#x6570;&#x636E;&#x7C7B;&#x578B;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fn</span>(<span class="hljs-params"></span>) </span>{}
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> fn);
&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;
&#x56E0;&#x4E3A;&#x51FD;&#x6570;&#x4E5F;&#x662F;&#x4E00;&#x79CD;&#x7C7B;&#x578B;&#xFF0C;&#x53EF;&#x4EE5;&#x628A;&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x4E24;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x5728;&#x53E6;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x4E2D;&#x8C03;&#x7528;
&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x8FD4;&#x56DE;&#x503C;
&#x56E0;&#x4E3A;&#x51FD;&#x6570;&#x662F;&#x4E00;&#x79CD;&#x7C7B;&#x578B;&#xFF0C;&#x6240;&#x4EE5;&#x53EF;&#x4EE5;&#x628A;&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x8FD4;&#x56DE;&#x503C;&#x4ECE;&#x51FD;&#x6570;&#x5185;&#x90E8;&#x8FD4;&#x56DE;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fn</span>(<span class="hljs-params">b</span>) </span>{
    <span class="hljs-keyword">var</span> a = <span class="hljs-number">10</span>;
    <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
        alert(a + b);
    }
}
fn(<span class="hljs-number">15</span>)();
</code></pre>
<h3 id="&#x9ED8;&#x8BA4;&#x53C2;&#x6570;"><a name="&#x9ED8;&#x8BA4;&#x53C2;&#x6570;" class="plugin-anchor" href="#&#x9ED8;&#x8BA4;&#x53C2;&#x6570;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x9ED8;&#x8BA4;&#x53C2;&#x6570;</h3>
<pre><code class="lang-javascript">ES5;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fn</span>(<span class="hljs-params">a, b</span>) </span>{
  <span class="hljs-built_in">console</span>.log(a, b || <span class="hljs-number">2</span>);
}
fn(<span class="hljs-number">1</span>); <span class="hljs-comment">// 1 2</span>
fn(<span class="hljs-number">1</span>, <span class="hljs-number">3</span>); <span class="hljs-comment">// 1 3</span>

ES6;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fn</span>(<span class="hljs-params">a, b = 2</span>) </span>{
  <span class="hljs-built_in">console</span>.log(a, b);
}
fn(<span class="hljs-number">1</span>); <span class="hljs-comment">// 1 2</span>
fn(<span class="hljs-number">1</span>, <span class="hljs-number">3</span>); <span class="hljs-comment">// 1 3</span>
</code></pre>
<h3 id="&#x7BAD;&#x5934;&#x51FD;&#x6570;"><a name="&#x7BAD;&#x5934;&#x51FD;&#x6570;" class="plugin-anchor" href="#&#x7BAD;&#x5934;&#x51FD;&#x6570;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x7BAD;&#x5934;&#x51FD;&#x6570;</h3>
<p>1&#x3001;&#x7BAD;&#x5934;&#x51FD;&#x6570;&#x4F7F;&#x5F97;&#x8868;&#x8FBE;&#x66F4;&#x52A0;&#x7B80;&#x6D01;&#x3002;
2&#x3001;&#x51FD;&#x6570;&#x4F53;&#x5185;&#x7684; this &#x5BF9;&#x8C61;&#xFF0C;&#x5C31;&#x662F;&#x5B9A;&#x4E49;&#x65F6;&#x6240;&#x5728;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x4F7F;&#x7528;&#x65F6;&#x6240;&#x5728;&#x7684;&#x5BF9;&#x8C61;&#x3002;
3&#x3001;&#x4E0D;&#x53EF;&#x4EE5;&#x5F53;&#x4F5C;&#x6784;&#x9020;&#x51FD;&#x6570;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x4E0D;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; new &#x547D;&#x4EE4;&#xFF0C;&#x5426;&#x5219;&#x4F1A;&#x629B;&#x51FA;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#x3002;
4&#x3001;&#x4E0D;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; arguments &#x5BF9;&#x8C61;&#xFF0C;&#x8BE5;&#x5BF9;&#x8C61;&#x5728;&#x51FD;&#x6570;&#x4F53;&#x5185;&#x4E0D;&#x5B58;&#x5728;&#x3002;&#x5982;&#x679C;&#x8981;&#x7528;&#xFF0C;&#x53EF;&#x4EE5;&#x7528; Rest &#x53C2;&#x6570;&#x4EE3;&#x66FF;&#x3002;
5&#x3001;&#x4E0D;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; yield &#x547D;&#x4EE4;&#xFF0C;&#x56E0;&#x6B64;&#x7BAD;&#x5934;&#x51FD;&#x6570;&#x4E0D;&#x80FD;&#x7528;&#x4F5C; Generator &#x51FD;&#x6570;&#x3002;</p>
<p>&lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD</p>
<h3 id="&#x8865;&#x5145;&#x8D44;&#x6599;"><a name="&#x8865;&#x5145;&#x8D44;&#x6599;" class="plugin-anchor" href="#&#x8865;&#x5145;&#x8D44;&#x6599;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x8865;&#x5145;&#x8D44;&#x6599;</h3>
<h1 id="&#x5B8C;&#x5168;&#x7406;&#x89E3;-js--arguments"><a name="&#x5B8C;&#x5168;&#x7406;&#x89E3;-js--arguments" class="plugin-anchor" href="#&#x5B8C;&#x5168;&#x7406;&#x89E3;-js--arguments"><i class="fa fa-link" aria-hidden="true"></i></a><a href="https://www.jianshu.com/p/e6bfa4bdf718" target="_blank">&#x5B8C;&#x5168;&#x7406;&#x89E3; JS--arguments</a></h1>
<h2 id="&#x51FD;&#x6570;&#x4E2D;-this-&#x89C4;&#x5219;"><a name="&#x51FD;&#x6570;&#x4E2D;-this-&#x89C4;&#x5219;" class="plugin-anchor" href="#&#x51FD;&#x6570;&#x4E2D;-this-&#x89C4;&#x5219;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x51FD;&#x6570;&#x4E2D; this &#x89C4;&#x5219;</h2>
<p>&#x7406;&#x89E3; this &#x4E4B;&#x524D;&#xFF0C; &#x5148;&#x7EA0;&#x6B63;&#x4E00;&#x4E2A;&#x89C2;&#x70B9;&#xFF0C;this &#x65E2;&#x4E0D;&#x6307;&#x5411;&#x51FD;&#x6570;&#x81EA;&#x8EAB;&#xFF0C;&#x4E5F;&#x4E0D;&#x6307;&#x51FD;&#x6570;&#x7684;&#x8BCD;&#x6CD5;&#x4F5C;&#x7528;&#x57DF;&#x3002;&#x5982;&#x679C;&#x4EC5;&#x901A;&#x8FC7; this &#x7684;&#x82F1;&#x6587;&#x89E3;&#x91CA;&#xFF0C;&#x592A;&#x5BB9;&#x6613;&#x4EA7;&#x751F;&#x8BEF;&#x5BFC;&#x4E86;&#x3002;&#x5B83;&#x5B9E;&#x9645;&#x662F;&#x5728;&#x51FD;&#x6570;&#x88AB;&#x8C03;&#x7528;&#x65F6;&#x624D;&#x53D1;&#x751F;&#x7684;&#x7ED1;&#x5B9A;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4; this &#x5177;&#x4F53;&#x6307;&#x5411;&#x4EC0;&#x4E48;&#xFF0C;&#x53D6;&#x51B3;&#x4E8E;&#x4F60;&#x662F;&#x600E;&#x4E48;&#x8C03;&#x7528;&#x7684;&#x51FD;&#x6570;&#x3002;</p>
<h2 id="this-&#x7684;&#x56DB;&#x79CD;&#x7ED1;&#x5B9A;&#x89C4;&#x5219;"><a name="this-&#x7684;&#x56DB;&#x79CD;&#x7ED1;&#x5B9A;&#x89C4;&#x5219;" class="plugin-anchor" href="#this-&#x7684;&#x56DB;&#x79CD;&#x7ED1;&#x5B9A;&#x89C4;&#x5219;"><i class="fa fa-link" aria-hidden="true"></i></a>this &#x7684;&#x56DB;&#x79CD;&#x7ED1;&#x5B9A;&#x89C4;&#x5219;</h2>
<p>this &#x7684; 4 &#x79CD;&#x7ED1;&#x5B9A;&#x89C4;&#x5219;&#x5206;&#x522B;&#x662F;&#xFF1A;&#x9ED8;&#x8BA4;&#x7ED1;&#x5B9A;&#x3001;&#x9690;&#x5F0F;&#x7ED1;&#x5B9A;&#x3001;&#x663E;&#x793A;&#x7ED1;&#x5B9A;&#x3001;new &#x7ED1;&#x5B9A;&#x3002;&#x4F18;&#x5148;&#x7EA7;&#x4ECE;&#x4F4E;&#x5230;&#x9AD8;&#x3002;</p>
<h2 id="&#x9ED8;&#x8BA4;&#x7ED1;&#x5B9A;"><a name="&#x9ED8;&#x8BA4;&#x7ED1;&#x5B9A;" class="plugin-anchor" href="#&#x9ED8;&#x8BA4;&#x7ED1;&#x5B9A;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x9ED8;&#x8BA4;&#x7ED1;&#x5B9A;</h2>
<p>&#x4EC0;&#x4E48;&#x53EB;&#x9ED8;&#x8BA4;&#x7ED1;&#x5B9A;&#xFF0C;&#x5373;&#x6CA1;&#x6709;&#x5176;&#x4ED6;&#x7ED1;&#x5B9A;&#x89C4;&#x5219;&#x5B58;&#x5728;&#x65F6;&#x7684;&#x9ED8;&#x8BA4;&#x89C4;&#x5219;&#x3002;&#x8FD9;&#x4E5F;&#x662F;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x4E2D;&#x6700;&#x5E38;&#x7528;&#x7684;&#x89C4;&#x5219;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{}
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>.a);

<span class="hljs-keyword">var</span> a = <span class="hljs-number">2</span>;
foo(); <span class="hljs-comment">// 2</span>
</code></pre>
<p>&#x56E0;&#x4E3A; foo()&#x662F;&#x76F4;&#x63A5;&#x8C03;&#x7528;&#x7684;&#xFF08;&#x72EC;&#x7ACB;&#x51FD;&#x6570;&#x8C03;&#x7528;&#xFF09;&#xFF0C;&#x6CA1;&#x6709;&#x5E94;&#x7528;&#x5176;&#x4ED6;&#x7684;&#x7ED1;&#x5B9A;&#x89C4;&#x5219;&#xFF0C;&#x8FD9;&#x91CC;&#x8FDB;&#x884C;&#x4E86;&#x9ED8;&#x8BA4;&#x7ED1;&#x5B9A;&#xFF0C;&#x5C06;&#x5168;&#x5C40;&#x5BF9;&#x8C61;&#x7ED1;&#x5B9A; this &#x4E0A;&#xFF0C;&#x6240;&#x4EE5; this.a &#x5C31;&#x89E3;&#x6790;&#x6210;&#x4E86;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x4E2D;&#x7684; a&#xFF0C;&#x5373; 2&#x3002;</p>
<h3 id="&#x6CE8;&#x610F;&#xFF1A;&#x5728;&#x4E25;&#x683C;&#x6A21;&#x5F0F;&#x4E0B;&#xFF08;strict-mode&#xFF09;&#xFF0C;&#x5168;&#x5C40;&#x5BF9;&#x8C61;&#x5C06;&#x65E0;&#x6CD5;&#x4F7F;&#x7528;&#x9ED8;&#x8BA4;&#x7ED1;&#x5B9A;&#xFF0C;&#x5373;&#x6267;&#x884C;&#x4F1A;&#x62A5;-undefined-&#x7684;&#x9519;&#x8BEF;"><a name="&#x6CE8;&#x610F;&#xFF1A;&#x5728;&#x4E25;&#x683C;&#x6A21;&#x5F0F;&#x4E0B;&#xFF08;strict-mode&#xFF09;&#xFF0C;&#x5168;&#x5C40;&#x5BF9;&#x8C61;&#x5C06;&#x65E0;&#x6CD5;&#x4F7F;&#x7528;&#x9ED8;&#x8BA4;&#x7ED1;&#x5B9A;&#xFF0C;&#x5373;&#x6267;&#x884C;&#x4F1A;&#x62A5;-undefined-&#x7684;&#x9519;&#x8BEF;" class="plugin-anchor" href="#&#x6CE8;&#x610F;&#xFF1A;&#x5728;&#x4E25;&#x683C;&#x6A21;&#x5F0F;&#x4E0B;&#xFF08;strict-mode&#xFF09;&#xFF0C;&#x5168;&#x5C40;&#x5BF9;&#x8C61;&#x5C06;&#x65E0;&#x6CD5;&#x4F7F;&#x7528;&#x9ED8;&#x8BA4;&#x7ED1;&#x5B9A;&#xFF0C;&#x5373;&#x6267;&#x884C;&#x4F1A;&#x62A5;-undefined-&#x7684;&#x9519;&#x8BEF;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x6CE8;&#x610F;&#xFF1A;&#x5728;&#x4E25;&#x683C;&#x6A21;&#x5F0F;&#x4E0B;&#xFF08;strict mode&#xFF09;&#xFF0C;&#x5168;&#x5C40;&#x5BF9;&#x8C61;&#x5C06;&#x65E0;&#x6CD5;&#x4F7F;&#x7528;&#x9ED8;&#x8BA4;&#x7ED1;&#x5B9A;&#xFF0C;&#x5373;&#x6267;&#x884C;&#x4F1A;&#x62A5; undefined &#x7684;&#x9519;&#x8BEF;</h3>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-meta">  &apos;use strict&apos;</span>;
  <span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>.a);
}

<span class="hljs-keyword">var</span> a = <span class="hljs-number">2</span>;
foo(); <span class="hljs-comment">// Uncaught TypeError: Cannot read property &apos;a&apos; of undefined</span>
</code></pre>
<h2 id="&#x9690;&#x5F0F;&#x7ED1;&#x5B9A;"><a name="&#x9690;&#x5F0F;&#x7ED1;&#x5B9A;" class="plugin-anchor" href="#&#x9690;&#x5F0F;&#x7ED1;&#x5B9A;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x9690;&#x5F0F;&#x7ED1;&#x5B9A;</h2>
<p>&#x9664;&#x4E86;&#x76F4;&#x63A5;&#x5BF9;&#x51FD;&#x6570;&#x8FDB;&#x884C;&#x8C03;&#x7528;&#x5916;&#xFF0C;&#x6709;&#x4E9B;&#x60C5;&#x51B5;&#x662F;&#xFF0C;&#x51FD;&#x6570;&#x7684;&#x8C03;&#x7528;&#x662F;&#x5728;&#x67D0;&#x4E2A;&#x5BF9;&#x8C61;&#x4E0A;&#x89E6;&#x53D1;&#x7684;&#xFF0C;&#x5373;&#x8C03;&#x7528;&#x4F4D;&#x7F6E;&#x4E0A;&#x5B58;&#x5728;&#x4E0A;&#x4E0B;&#x6587;&#x5BF9;&#x8C61;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>.a);
}

<span class="hljs-keyword">var</span> a = <span class="hljs-number">2</span>;

<span class="hljs-keyword">var</span> obj = {
  a: <span class="hljs-number">3</span>,
  foo: foo
};

obj.foo(); <span class="hljs-comment">// 3</span>
</code></pre>
<p>&#x91CC; foo &#x51FD;&#x6570;&#x88AB;&#x5F53;&#x505A;&#x5F15;&#x7528;&#x5C5E;&#x6027;&#xFF0C;&#x88AB;&#x6DFB;&#x52A0;&#x5230; obj &#x5BF9;&#x8C61;&#x4E0A;&#x3002;&#x8FD9;&#x91CC;&#x7684;&#x8C03;&#x7528;&#x8FC7;&#x7A0B;&#x662F;&#x8FD9;&#x6837;&#x7684;&#xFF1A;
&#x83B7;&#x53D6; obj.foo &#x5C5E;&#x6027; -&gt; &#x6839;&#x636E;&#x5F15;&#x7528;&#x5173;&#x7CFB;&#x627E;&#x5230; foo &#x51FD;&#x6570;&#xFF0C;&#x6267;&#x884C;&#x8C03;&#x7528;
&#x6240;&#x4EE5;&#x8FD9;&#x91CC;&#x5BF9; foo &#x7684;&#x8C03;&#x7528;&#x5B58;&#x5728;&#x4E0A;&#x4E0B;&#x6587;&#x5BF9;&#x8C61; obj&#xFF0C;this &#x8FDB;&#x884C;&#x4E86;&#x9690;&#x5F0F;&#x7ED1;&#x5B9A;&#xFF0C;&#x5373; this &#x7ED1;&#x5B9A;&#x5230;&#x4E86; obj &#x4E0A;&#xFF0C;&#x6240;&#x4EE5; this.a &#x88AB;&#x89E3;&#x6790;&#x6210;&#x4E86; obj.a&#xFF0C;&#x5373; 3&#x3002;</p>
<h2 id="&#x591A;&#x5C42;&#x8C03;&#x7528;&#x94FE;"><a name="&#x591A;&#x5C42;&#x8C03;&#x7528;&#x94FE;" class="plugin-anchor" href="#&#x591A;&#x5C42;&#x8C03;&#x7528;&#x94FE;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x591A;&#x5C42;&#x8C03;&#x7528;&#x94FE;</h2>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>.a);
}
<span class="hljs-keyword">var</span> a = <span class="hljs-number">2</span>;
<span class="hljs-keyword">var</span> obj1 = {
  a: <span class="hljs-number">4</span>,
  foo: foo
};
<span class="hljs-keyword">var</span> obj2 = {
  a: <span class="hljs-number">3</span>,
  obj1: obj1
};
obj2.obj1.foo(); <span class="hljs-comment">//4</span>
</code></pre>
<p>&#x5148;&#x83B7;&#x53D6; obj1.obj2 -&gt; &#x901A;&#x8FC7;&#x5F15;&#x7528;&#x83B7;&#x53D6;&#x5230; obj2 &#x5BF9;&#x8C61;&#xFF0C;&#x518D;&#x8BBF;&#x95EE; obj2.foo -&gt; &#x6700;&#x540E;&#x6267;&#x884C; foo &#x51FD;&#x6570;&#x8C03;&#x7528;
&#x8FD9;&#x91CC;&#x8C03;&#x7528;&#x94FE;&#x4E0D;&#x53EA;&#x4E00;&#x5C42;&#xFF0C;&#x5B58;&#x5728; obj1&#x3001;obj2 &#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x90A3;&#x4E48;&#x9690;&#x5F0F;&#x7ED1;&#x5B9A;&#x5177;&#x4F53;&#x4F1A;&#x7ED1;&#x54EA;&#x4E2A;&#x5BF9;&#x8C61;&#x3002;&#x8FD9;&#x91CC;&#x539F;&#x5219;&#x662F;&#x83B7;&#x53D6;&#x6700;&#x540E;&#x4E00;&#x5C42;&#x8C03;&#x7528;&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#x5BF9;&#x8C61;&#xFF0C;&#x5373; obj2&#xFF0C;&#x6240;&#x4EE5;&#x7ED3;&#x679C;&#x663E;&#x7136;&#x662F; 4&#xFF08;obj2.a&#xFF09;&#x3002;</p>
<h2 id="&#x9690;&#x5F0F;&#x4E22;&#x5931;&#xFF08;&#x51FD;&#x6570;&#x522B;&#x540D;&#xFF09;"><a name="&#x9690;&#x5F0F;&#x4E22;&#x5931;&#xFF08;&#x51FD;&#x6570;&#x522B;&#x540D;&#xFF09;" class="plugin-anchor" href="#&#x9690;&#x5F0F;&#x4E22;&#x5931;&#xFF08;&#x51FD;&#x6570;&#x522B;&#x540D;&#xFF09;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x9690;&#x5F0F;&#x4E22;&#x5931;&#xFF08;&#x51FD;&#x6570;&#x522B;&#x540D;&#xFF09;</h2>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>.a);
}
<span class="hljs-keyword">var</span> a = <span class="hljs-number">2</span>;
<span class="hljs-keyword">var</span> obj = {
  a: <span class="hljs-number">3</span>,
  foo: foo
};
<span class="hljs-keyword">var</span> bar = obj.foo;
bar(); <span class="hljs-comment">//2</span>
</code></pre>
<p>obj.foo &#x8D4B;&#x503C;&#x7ED9; bar&#xFF0C;&#x90A3;&#x8C03;&#x7528; bar()&#x4E3A;&#x4EC0;&#x4E48;&#x6CA1;&#x6709;&#x89E6;&#x53D1;&#x9690;&#x5F0F;&#x7ED1;&#x5B9A;&#xFF0C;&#x4F7F;&#x7528;&#x7684;&#x662F;&#x9ED8;&#x8BA4;&#x7ED1;&#x5B9A;&#x5462;&#x3002;
&#x8FD9;&#x91CC;&#x6709;&#x4E2A;&#x6982;&#x5FF5;&#x8981;&#x7406;&#x89E3;&#x6E05;&#x695A;&#xFF0C;obj.foo &#x662F;&#x5F15;&#x7528;&#x5C5E;&#x6027;&#xFF0C;&#x8D4B;&#x503C;&#x7ED9; bar &#x7684;&#x5B9E;&#x9645;&#x4E0A;&#x5C31;&#x662F; foo &#x51FD;&#x6570;&#xFF08;&#x5373;&#xFF1A;bar &#x6307;&#x5411; foo &#x672C;&#x8EAB;&#xFF09;&#x3002;
&#x90A3;&#x4E48;&#xFF0C;&#x5B9E;&#x9645;&#x7684;&#x8C03;&#x7528;&#x5173;&#x7CFB;&#x662F;&#xFF1A;&#x901A;&#x8FC7; bar &#x627E;&#x5230; foo &#x51FD;&#x6570;&#xFF0C;&#x8FDB;&#x884C;&#x8C03;&#x7528;&#x3002;&#x6574;&#x4E2A;&#x8C03;&#x7528;&#x8FC7;&#x7A0B;&#x5E76;&#x6CA1;&#x6709; obj &#x7684;&#x53C2;&#x6570;&#xFF0C;&#x6240;&#x4EE5;&#x662F;&#x9ED8;&#x8BA4;&#x7ED1;&#x5B9A;&#xFF0C;&#x5168;&#x5C40;&#x5C5E;&#x6027; a</p>
<h2 id="&#x9690;&#x5F0F;&#x4E22;&#x5931;&#xFF08;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF09;"><a name="&#x9690;&#x5F0F;&#x4E22;&#x5931;&#xFF08;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF09;" class="plugin-anchor" href="#&#x9690;&#x5F0F;&#x4E22;&#x5931;&#xFF08;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF09;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x9690;&#x5F0F;&#x4E22;&#x5931;&#xFF08;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF09;</h2>
<pre><code class="lang-javascript">unction foo() {
    <span class="hljs-built_in">console</span>.log( <span class="hljs-keyword">this</span>.a );
}
<span class="hljs-keyword">var</span> a = <span class="hljs-number">2</span>;
<span class="hljs-keyword">var</span> obj = {
    a: <span class="hljs-number">3</span>,
    foo: foo
};
setTimeout( obj.foo, <span class="hljs-number">100</span> ); <span class="hljs-comment">// 2</span>
</code></pre>
<p>&#x867D;&#x7136;&#x53C2;&#x4F20;&#x662F; obj.foo&#xFF0C;&#x56E0;&#x4E3A;&#x662F;&#x5F15;&#x7528;&#x5173;&#x7CFB;&#xFF0C;&#x6240;&#x4EE5;&#x4F20;&#x53C2;&#x5B9E;&#x9645;&#x4E0A;&#x4F20;&#x7684;&#x5C31;&#x662F; foo &#x5BF9;&#x8C61;&#x672C;&#x8EAB;&#x7684;&#x5F15;&#x7528;&#x3002;&#x5BF9;&#x4E8E; setTimeout &#x7684;&#x8C03;&#x7528;&#xFF0C;&#x8FD8;&#x662F; setTimeout -&gt; &#x83B7;&#x53D6;&#x53C2;&#x6570;&#x4E2D; foo &#x7684;&#x5F15;&#x7528;&#x53C2;&#x6570; -&gt; &#x6267;&#x884C; foo &#x51FD;&#x6570;&#xFF0C;&#x4E2D;&#x95F4;&#x6CA1;&#x6709; obj &#x7684;&#x53C2;&#x4E0E;&#x3002;&#x8FD9;&#x91CC;&#x4F9D;&#x65E7;&#x8FDB;&#x884C;&#x7684;&#x662F;&#x9ED8;&#x8BA4;&#x7ED1;&#x5B9A;</p>
<h2 id="&#x663E;&#x793A;&#x7ED1;&#x5B9A;"><a name="&#x663E;&#x793A;&#x7ED1;&#x5B9A;" class="plugin-anchor" href="#&#x663E;&#x793A;&#x7ED1;&#x5B9A;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x663E;&#x793A;&#x7ED1;&#x5B9A;</h2>
<p>&#x76F8;&#x5BF9;&#x9690;&#x5F0F;&#x7ED1;&#x5B9A;&#xFF0C;this &#x503C;&#x5728;&#x8C03;&#x7528;&#x8FC7;&#x7A0B;&#x4E2D;&#x4F1A;&#x52A8;&#x6001;&#x53D8;&#x5316;&#xFF0C;&#x53EF;&#x662F;&#x6211;&#x4EEC;&#x5C31;&#x60F3;&#x7ED1;&#x5B9A;&#x6307;&#x5B9A;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x8FD9;&#x65F6;&#x5C31;&#x7528;&#x5230;&#x4E86;&#x663E;&#x793A;&#x7ED1;&#x5B9A;&#x3002;
&#x663E;&#x793A;&#x7ED1;&#x5B9A;&#x4E3B;&#x8981;&#x662F;&#x901A;&#x8FC7;&#x6539;&#x53D8;&#x5BF9;&#x8C61;&#x7684; prototype &#x5173;&#x8054;&#x5BF9;&#x8C61;&#xFF0C;&#x8FD9;&#x91CC;&#x4E0D;&#x5C55;&#x5F00;&#x8BB2;&#x3002;&#x5177;&#x4F53;&#x4F7F;&#x7528;&#x4E0A;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x8FD9;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5; call(&#x2026;)&#x6216; apply(&#x2026;)&#x6765;&#x5B9E;&#x73B0;&#xFF08;&#x5927;&#x591A;&#x6570;&#x51FD;&#x6570;&#x53CA;&#x81EA;&#x5DF1;&#x521B;&#x5EFA;&#x7684;&#x51FD;&#x6570;&#x9ED8;&#x8BA4;&#x90FD;&#x63D0;&#x4F9B;&#x8FD9;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#xFF09;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>.a);
}
<span class="hljs-keyword">var</span> a = <span class="hljs-number">2</span>;
<span class="hljs-keyword">var</span> obj1 = {
  a: <span class="hljs-number">3</span>
};
<span class="hljs-keyword">var</span> obj2 = {
  a: <span class="hljs-number">4</span>
};
foo.call(obj1); <span class="hljs-comment">// 3</span>
foo.call(obj2); <span class="hljs-comment">// 4</span>
</code></pre>
<h2 id="new-&#x7ED1;&#x5B9A;"><a name="new-&#x7ED1;&#x5B9A;" class="plugin-anchor" href="#new-&#x7ED1;&#x5B9A;"><i class="fa fa-link" aria-hidden="true"></i></a>new &#x7ED1;&#x5B9A;</h2>
<p>&#x4F7F;&#x7528; new &#x6765;&#x8C03;&#x7528;&#x51FD;&#x6570;&#xFF0C;&#x4F1A;&#x81EA;&#x52A8;&#x6267;&#x884C;&#x5982;&#x4E0B;&#x64CD;&#x4F5C;&#xFF1A;
&#x5982;&#x679C;&#x51FD;&#x6570;&#x6CA1;&#x6709;&#x8FD4;&#x56DE;&#x5176;&#x4ED6;&#x5BF9;&#x8C61;,&#x90A3;&#x4E48; new &#x8868;&#x8FBE;&#x5F0F;&#x4E2D;&#x7684;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x4F1A;&#x81EA;&#x52A8;&#x8FD4;&#x56DE;&#x8FD9;&#x4E2A;&#x65B0;&#x5BF9;&#x8C61;&#x3002;
&#x4ECE;&#x7B2C;&#x4E09;&#x70B9;&#x53EF;&#x4EE5;&#x770B;&#x51FA;&#xFF0C;this &#x6307;&#x5411;&#x7684;&#x5C31;&#x662F;&#x5BF9;&#x8C61;&#x672C;&#x8EAB;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params">a</span>) </span>{
  <span class="hljs-keyword">this</span>.a = a;
}
<span class="hljs-keyword">var</span> a = <span class="hljs-number">2</span>;
<span class="hljs-keyword">var</span> bar1 = <span class="hljs-keyword">new</span> foo(<span class="hljs-number">3</span>);
<span class="hljs-built_in">console</span>.log(bar1.a); <span class="hljs-comment">// 3</span>

<span class="hljs-keyword">var</span> bar2 = <span class="hljs-keyword">new</span> foo(<span class="hljs-number">4</span>);
<span class="hljs-built_in">console</span>.log(bar2.a); <span class="hljs-comment">// 4</span>
</code></pre>
<p>&#x56E0;&#x4E3A;&#x6BCF;&#x6B21;&#x8C03;&#x7528;&#x751F;&#x6210;&#x7684;&#x662F;&#x5168;&#x65B0;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x8BE5;&#x5BF9;&#x8C61;&#x53C8;&#x4F1A;&#x81EA;&#x52A8;&#x7ED1;&#x5B9A;&#x5230; this &#x4E0A;</p>
<p>&#x6570;&#x662F;&#x5426;&#x5728; new &#x4E2D;&#x8C03;&#x7528;(new &#x7ED1;&#x5B9A;)?&#x5982;&#x679C;&#x662F;&#x7684;&#x8BDD; this &#x7ED1;&#x5B9A;&#x7684;&#x662F;&#x65B0;&#x521B;&#x5EFA;&#x7684;&#x5BF9;&#x8C61;&#x3002;
&#x6570;&#x662F;&#x5426;&#x901A;&#x8FC7; call&#x3001;apply(&#x663E;&#x5F0F;&#x7ED1;&#x5B9A;)&#x6216;&#x8005;&#x786C;&#x7ED1;&#x5B9A;&#x8C03;&#x7528;?&#x5982;&#x679C;&#x662F;&#x7684;&#x8BDD;,this &#x7ED1;&#x5B9A;&#x7684;&#x662F; &#x6307;&#x5B9A;&#x7684;&#x5BF9;&#x8C61;&#x3002;
&#x6570;&#x662F;&#x5426;&#x5728;&#x67D0;&#x4E2A;&#x4E0A;&#x4E0B;&#x6587;&#x5BF9;&#x8C61;&#x4E2D;&#x8C03;&#x7528;(&#x9690;&#x5F0F;&#x7ED1;&#x5B9A;)?&#x5982;&#x679C;&#x662F;&#x7684;&#x8BDD;,this &#x7ED1;&#x5B9A;&#x7684;&#x662F;&#x90A3;&#x4E2A;&#x4E0A;&#x4E0B;&#x6587;&#x5BF9;&#x8C61;&#x3002;
&#x679C;&#x90FD;&#x4E0D;&#x662F;&#x7684;&#x8BDD;,&#x4F7F;&#x7528;&#x9ED8;&#x8BA4;&#x7ED1;&#x5B9A;&#x3002;&#x5982;&#x679C;&#x5728;&#x4E25;&#x683C;&#x6A21;&#x5F0F;&#x4E0B;,&#x5C31;&#x7ED1;&#x5B9A;&#x5230; undefined,&#x5426;&#x5219;&#x7ED1;&#x5B9A;&#x5230; &#x5168;&#x5C40;&#x5BF9;&#x8C61;&#x3002;</p>
<h2 id="&#x7279;&#x4F8B;"><a name="&#x7279;&#x4F8B;" class="plugin-anchor" href="#&#x7279;&#x4F8B;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x7279;&#x4F8B;</h2>
<p>&#x5728;&#x663E;&#x793A;&#x7ED1;&#x5B9A;&#x4E2D;&#xFF0C;&#x5BF9;&#x4E8E; null &#x548C; undefined &#x7684;&#x7ED1;&#x5B9A;&#x5C06;&#x4E0D;&#x4F1A;&#x751F;&#x6548;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>.a);
}
foo.call(<span class="hljs-literal">null</span>); <span class="hljs-comment">// 2</span>
foo.call(<span class="hljs-literal">undefined</span>); <span class="hljs-comment">// 2</span>
</code></pre>
<p>&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E3B;&#x8981;&#x662F;&#x7528;&#x5728;&#x4E0D;&#x5173;&#x5FC3; this &#x7684;&#x5177;&#x4F53;&#x7ED1;&#x5B9A;&#x5BF9;&#x8C61;&#xFF08;&#x7528;&#x6765;&#x5FFD;&#x7565; this&#xFF09;&#xFF0C;&#x800C;&#x4F20;&#x5165; null &#x5B9E;&#x9645;&#x4E0A;&#x4F1A;&#x8FDB;&#x884C;&#x9ED8;&#x8BA4;&#x7ED1;&#x5B9A;&#xFF0C;&#x5BFC;&#x81F4;&#x51FD;&#x6570;&#x4E2D;&#x53EF;&#x80FD;&#x4F1A;&#x4F7F;&#x7528;&#x5230;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#xFF0C;&#x4E0E;&#x9884;&#x671F;&#x4E0D;&#x7B26;&#x3002;
&#x6240;&#x4EE5;&#x5BF9;&#x4E8E;&#x8981;&#x5FFD;&#x7565; this &#x7684;&#x60C5;&#x51B5;&#xFF0C;&#x53EF;&#x4EE5;&#x4F20;&#x5165;&#x4E00;&#x4E2A;&#x7A7A;&#x5BF9;&#x8C61; &#xF8;&#xFF0C;&#x8BE5;&#x5BF9;&#x8C61;&#x901A;&#x8FC7; Object.create(null)&#x521B;&#x5EFA;&#x3002;&#x8FD9;&#x91CC;&#x4E0D;&#x7528;{}&#x7684;&#x539F;&#x56E0;&#x662F;&#xFF0C;&#xF8; &#x662F;&#x771F;&#x6B63;&#x610F;&#x4E49;&#x4E0A;&#x7684;&#x7A7A;&#x5BF9;&#x8C61;&#xFF0C;&#x5B83;&#x4E0D;&#x521B;&#x5EFA; Object.prototype &#x59D4;&#x6258;&#xFF0C;{}&#x548C;&#x666E;&#x901A;&#x5BF9;&#x8C61;&#x4E00;&#x6837;&#xFF0C;&#x6709;&#x539F;&#x578B;&#x94FE;&#x59D4;&#x6258;&#x5173;&#x7CFB;&#x3002;</p>
<h2 id="&#x7BAD;&#x5934;&#x51FD;&#x6570;"><a name="&#x7BAD;&#x5934;&#x51FD;&#x6570;" class="plugin-anchor" href="#&#x7BAD;&#x5934;&#x51FD;&#x6570;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x7BAD;&#x5934;&#x51FD;&#x6570;</h2>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>.a);
}
<span class="hljs-keyword">var</span> a = <span class="hljs-number">2</span>;
<span class="hljs-keyword">var</span> obj = {
  a: <span class="hljs-number">3</span>,
  foo: foo
};
obj.foo(); <span class="hljs-comment">//3</span>
</code></pre>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> foo = () =&gt; {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>.a);
};
<span class="hljs-keyword">var</span> a = <span class="hljs-number">2</span>;
<span class="hljs-keyword">var</span> obj = {
  a: <span class="hljs-number">3</span>,
  foo: foo
};
obj.foo(); <span class="hljs-comment">//2</span>
foo.call(obj); <span class="hljs-comment">//2 &#xFF0C;&#x7BAD;&#x5934;&#x51FD;&#x6570;&#x4E2D;&#x663E;&#x793A;&#x7ED1;&#x5B9A;&#x4E0D;&#x4F1A;&#x751F;&#x6548;</span>
</code></pre>
<h2 id="&#x51FD;&#x6570;&#x56DE;&#x8C03;&#x4E2D;&#x7BAD;&#x5934;&#x51FD;&#x6570;"><a name="&#x51FD;&#x6570;&#x56DE;&#x8C03;&#x4E2D;&#x7BAD;&#x5934;&#x51FD;&#x6570;" class="plugin-anchor" href="#&#x51FD;&#x6570;&#x56DE;&#x8C03;&#x4E2D;&#x7BAD;&#x5934;&#x51FD;&#x6570;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x51FD;&#x6570;&#x56DE;&#x8C03;&#x4E2D;&#x7BAD;&#x5934;&#x51FD;&#x6570;</h2>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>.a);
  };
}
<span class="hljs-keyword">var</span> a = <span class="hljs-number">2</span>;
<span class="hljs-keyword">var</span> obj = {
  a: <span class="hljs-number">3</span>,
  foo: foo
};
<span class="hljs-keyword">var</span> bar = obj.foo();
bar(); <span class="hljs-comment">//2</span>
</code></pre>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">foo</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> () =&gt; {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>.a);
  };
}
<span class="hljs-keyword">var</span> a = <span class="hljs-number">2</span>;
<span class="hljs-keyword">var</span> obj = {
  a: <span class="hljs-number">3</span>,
  foo: foo
};
<span class="hljs-keyword">var</span> bar = obj.foo();
bar(); <span class="hljs-comment">//3</span>
</code></pre>
<p>&#x7BAD;&#x5934;&#x51FD;&#x6570;&#x7684; this &#x7ED1;&#x5B9A;&#x53EA;&#x53D6;&#x51B3;&#x4E8E;&#x5916;&#x5C42;&#xFF08;&#x51FD;&#x6570;&#x6216;&#x5168;&#x5C40;&#xFF09;&#x7684;&#x4F5C;&#x7528;&#x57DF;</p>
<h4 id="this-&#x56FE;&#x89E3;"><a name="this-&#x56FE;&#x89E3;" class="plugin-anchor" href="#this-&#x56FE;&#x89E3;"><i class="fa fa-link" aria-hidden="true"></i></a>this &#x56FE;&#x89E3;</h4>
<p><img src="../../img/this.png" alt="markdown"></p>
<p>MDN: &#x7BAD;&#x5934;&#x51FD;&#x6570;&#x8868;&#x8FBE;&#x5F0F; <a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions/Arrow_functions" target="_blank">https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions/Arrow_functions</a>
MDN: &#x51FD;&#x6570;&#x7684; this <a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/this" target="_blank">https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/this</a>
segmentfault <a href="https://segmentfault.com/a/1190000008400124" target="_blank">https://segmentfault.com/a/1190000008400124</a></p>
<h3 id="js-&#x8D44;&#x6599;"><a name="js-&#x8D44;&#x6599;" class="plugin-anchor" href="#js-&#x8D44;&#x6599;"><i class="fa fa-link" aria-hidden="true"></i></a>js &#x8D44;&#x6599;</h3>
<p>js &#x7CFB;&#x5217;&#x535A;&#x5BA2; <a href="https://www.cnblogs.com/wangfupeng1988/p/3977924.html" target="_blank">https://www.cnblogs.com/wangfupeng1988/p/3977924.html</a></p>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<blockquote>
<p>6bdfa5ec21202e46cee2dc5711b29167ef94b742</p>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; wml 2019 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2020-05-15 11:32:12
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="object.html" class="navigation navigation-prev " aria-label="Previous page: 对象">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="string.html" class="navigation navigation-next " aria-label="Next page: 字符串">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"函数","level":"1.3.3","depth":2,"next":{"title":"字符串","level":"1.3.4","depth":2,"path":"category/js/string.md","ref":"category/js/string.md","articles":[]},"previous":{"title":"对象","level":"1.3.2","depth":2,"path":"category/js/object.md","ref":"category/js/object.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-lunr","anchors","sitemap-general","toggle-chapters","expandable-chapters","splitter","edit-link","advanced-emoji","tbfed-pagefooter"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy wml 2019","modify_label":"该文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"github":{"url":"https://gitee.com/chengdaohua/remarkBook"},"simple-page-toc":{"maxDepth":3,"skipFirstH1":false},"splitter":{},"search-pro":{"cutWordLib":"nodejieba","defineWord":["gitbook-use"]},"search":{},"donate":{"wechat":"","alipay":"","title":"","button":"赏","alipayText":"支付宝打赏","wechatText":"微信打赏"},"sitemap-general":{"prefix":"https://chengdaohua.gitee.io/remarkbook/"},"changyan":{"appid":"cytowLiqM","conf":"the conf in the code generate by changyan"},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"sitemap":{"hostname":"https://wmlhub.gitee.io/gitbook/"},"baidu":{"token":"084b900a1e26f6bae60e09d18e3aa455"},"advanced-emoji":{"embedEmojis":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":true,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"edit-link":{"label":"Edit This Page","base":"https://gitee.com/chengdaohua/remarkBook"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"anchors":{},"toggle-chapters":{},"expandable-chapters":{}},"theme":"default","author":"chengdaohua","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"前端学习笔记","language":"cn","links":{"sidebar":{"Home":"https://chengdaohua.gitee.io/remarkbook/"}},"gitbook":"3.2.3","description":"记录日常学习总结"},"file":{"path":"category/js/function.md","mtime":"2020-05-15T03:32:12.781Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-05-22T06:18:21.707Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-toggle-chapters/toggle.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-edit-link/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

